/**
 * Created by lzfto
 * Created time: 2025/05/25
 */
package org.lzfto.flink.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.lzfto.flink.demo.common.Configuration;
import org.lzfto.flink.demo.table.service.FlinkConnectorTestService;
import org.lzfto.flink.demo.table.service.impl.CsvFileConnectorTest;
import org.lzfto.flink.demo.table.service.impl.CustomJdbcConnectorTest;
import org.lzfto.flink.demo.table.service.impl.StandardJdbcConnectorTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DemoApp {

	private static Logger logger = LoggerFactory.getLogger(DemoApp.class);
	private List<Task> tasks;

	public DemoApp(List<Task> tasks) {
		this.tasks = tasks;
	}
	public DemoApp(){
		this.tasks = new ArrayList<>();
		this.tasks.add(new Task("加载一个csv，并打印其内容", 1));
		this.tasks.add(new Task("流模式下，从一个数据库获取数据并插入到另外一个数据库", 2));
		this.tasks.add(new Task("批处理模式下，从一个数据库获取数据并插入到另外一个数据库,插入前清理目标表", 3));
	}

	public static void main(String[] args) {
		Configuration.init();
		DemoApp demo = new DemoApp();
		Scanner scanner = new Scanner(System.in);

		while (true) {
			System.out.println("\n请选择要执行的测试(输入-1退出):");
			demo.tasks.forEach(task ->
				System.out.printf("%d. %s\n", task.no(), task.name()));
			System.out.print("请输入选项: ");

			int choice = scanner.nextInt();
			if (choice == -1) {
				break;
			}
			try {
				FlinkConnectorTestService test = switch(choice) {
					case 1 -> new CsvFileConnectorTest();
					case 2 -> new StandardJdbcConnectorTest();
					case 3 -> new CustomJdbcConnectorTest();
					default -> throw new IllegalArgumentException("无效的选项");
				};
				test.run();
			} catch (Exception e) {
				System.err.println("执行出错: " + e.getMessage());
				logger.error("执行出错", e);
			}
		}

		scanner.close();
		System.out.println("程序已退出");
	}

	record Task(String name, int no) {}
}


